﻿using System;
using System.Diagnostics;

using ppatierno.AzureSBLite.Messaging;

namespace Eldert.IoT.RaspberryPi2.Ship
{
    /// <summary>
    /// Class used to handle exceptions.
    /// </summary>
    public static class Exceptions
    {
        private static QueueClient _queue;

        private static QueueClient Queue => _queue ?? (_queue = QueueClient.CreateFromConnectionString(Settings.Read("ErrorsQueueConnectionString"), Settings.Read("ErrorsQueue")));

        public static void WriteToDebugConsole(this Exception exception)
        {
            Debug.WriteLine(exception.Message);
        }

        public static void WriteToServiceBusQueue(this Exception exception)
        {
            var message = new BrokeredMessage();
            message.Properties["time"] = DateTime.UtcNow;
            message.Properties["exceptionmessage"] = exception.Message;
            Queue.Send(message);
        }

        public static void Log(this Exception exception)
        {
            exception.WriteToDebugConsole();
            exception.WriteToServiceBusQueue();
        }
    }
}
